package com.bsoft.template.web.assets;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsoft.template.common.Result;
import com.bsoft.template.common.auth.UserInfo;
import com.bsoft.template.entity.assets.AssetsLb;
import com.bsoft.template.entity.auth.User;
import com.bsoft.template.service.assets.AssetsLbService;
import com.bsoft.template.service.auth.UserService;
import com.bsoft.template.util.RequestParamPaser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("assetsLb")
public class AssetsLbController {
    @Autowired
    private AssetsLbService assetsLbService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfo userInfo;

    @RolesAllowed({"ROLE_dandang", "ROLE_zhixing"})
    @PostMapping("/save")
    public Result save(@RequestBody AssetsLb assetsLb) {
        if ("02".equals(assetsLb.getZt())) {
            return new Result().fail();
        }
        if (assetsLb.getPzh() == null) {

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String date = LocalDate.now().format(formatter);
            String maxId = assetsLbService.getMaxId(date.substring(0,4));
            String surfix = "0001";
            if (StringUtils.isNotBlank(maxId)) {
                surfix = String.format("%04d", Integer.parseInt(maxId.substring(8)) + 1);
            }
            assetsLb.setPzh(date + surfix);

            User user = userInfo.getUser();
            assetsLb.setZdr(user.getUsername());
            assetsLb.setZdrq(new Date());
            assetsLb.setZtrq(new Date());
            assetsLb.setDwfzr(userService.getUserByRolecode("fuzong"));

            return assetsLbService.save(assetsLb);
        }else{
            return assetsLbService.updateById(assetsLb);
        }

    }

    @GetMapping("/getList")
    public Result getList(HttpServletRequest request) {
        Map<String, String> params = RequestParamPaser.getParameters(request);
        IPage<AssetsLb> page = new Page<>(
                Long.parseLong(params.getOrDefault("page", "1")),
                Long.parseLong(params.getOrDefault("pageSize", "10"))
        );
        QueryWrapper<AssetsLb> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(params.get("pzh"))) {
            wrapper.like("pzh", params.get("pzh"));
        }
        if (StringUtils.isNotBlank(params.get("zclb"))) {
            wrapper.eq("zclb", params.get("zclb"));
        }
        if (StringUtils.isNotBlank(params.get("mc"))) {
            wrapper.eq("mc", params.get("mc"));
        }
        if (StringUtils.isNotBlank(params.get("cfd"))) {
            wrapper.eq("cfd", params.get("cfd"));
        }
        if (StringUtils.isNotBlank(params.get("fjh"))) {
            wrapper.eq("fjh", params.get("fjh"));
        }
        if (StringUtils.isNotBlank(params.get("zt"))) {
            wrapper.eq("zt", params.get("zt"));
        }
        if (StringUtils.isNotBlank(params.get("synx"))) {
            wrapper.eq("synx", params.get("synx"));
        }
        wrapper.orderByDesc("pzh");
//        if (StringUtils.isNotBlank(params.get("code"))) {
//            wrapper.like("code", params.get("code"))
//                    .or().like("description", params.get("code"));
//        }
        return assetsLbService.getListByPage(page, wrapper);
    }

    @GetMapping("remove")
    public Result remove(String pzh) {
        return assetsLbService.removeById(pzh);
    }

    @GetMapping("/getAssetsLb")
    public Result getAssetsLb(String pzh) {
        QueryWrapper<AssetsLb> wrapper = new QueryWrapper<>();
        wrapper.eq("pzh", pzh);
        return assetsLbService.getOne(wrapper);
    }

    @GetMapping("updateZt")
    public Result updateZt(String pzh, String zt, String sm) {
        User user = userInfo.getUser();
        UpdateWrapper<AssetsLb> wrapper = new UpdateWrapper<>();
        wrapper.eq("pzh", pzh);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = LocalDate.now().format(formatter);
        String setSql = "zt='"+ zt+"',ztrq = '"+date+"'";
        if ("02".equals(zt)) {
            setSql = setSql+",JBR='"+user.getUsername()+"'";
        }
        if ("03".equals(zt) && StringUtils.isNotBlank(sm)) {
            setSql = setSql + ",BZ=concat(BZ, ' " + sm + "')";
        }
        wrapper.setSql(setSql);
        return assetsLbService.update(wrapper);
    }

    @GetMapping("getExpire")
    public Result getExpire() {
        return assetsLbService.getExpire();
    }

    @GetMapping("getAuditCount")
    public Result getAuditCount() {
        return assetsLbService.getAuditCount();
    }

    @GetMapping("exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        assetsLbService.exportExcel(request, response);
    }

    @GetMapping("exportExcelAll")
    public void exportExcelAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        assetsLbService.exportExcelAll(request, response);
    }

}
